package com.handinfo.uimenu.window;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.GameScene;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.test.touch.GameTouchManager;
import com.test.touch.TouchListener;
import com.test.touch.TouchRect;

public class UISetSystem extends UIAbstractWindow
{
	private final int STATE_SYS = 1;
//	private final int STATE_CHAT = 2;
	private final int STATE_DIS = 3;
	//
	private final int TYPE_OC = 0;//开关
	private final int TYPE_HML = 1;//高中低
	private final int TYPE_NUM = 2;//数字
	private final int TYPE_STR = 3;//文字
	//
	private final int INDEX_TYPE = 0;
	private final int INDEX_DATE = 1;
	//
	private byte[][][] m_datas;// 0 type , 1 data
	private int m_index;
	private final int H_RECT = UITools.H_RECT - (UITools.H_LABEL + UITools.IH_RECTS) ;
	
//	void registerLoginBtn(int[][] rect) {
//		if (rect == null)
//			return;
//		int len = rect.length;
//		for (byte i=0;i<len;i++) {
//			final byte select = i;
//			GameTouchManager.addStaticRect(new TouchRect(rect[i], new TouchListener() {
//				
//				public void doTouch(int x, int y) {
//					changeState(STATE_SYS + select);
////					AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.MIDDLE);
//				}
//			}));
//		}
//	}
//	void registerContentBtn(int[][] rect) {
//		if (rect == null)
//			return;
//		int len = rect.length;
//		for (byte i=0;i<len;i++) {
//			final byte select = i;
//			GameTouchManager.addStaticRect(new TouchRect(rect[i], new TouchListener() {
//				
//				public void doTouch(int x, int y) {
//					if (m_index == select) {
//						AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.MIDDLE);	
//					} else {
//						m_index = select;
//					}
//				}
//			}));
//		}
//	}
	public UISetSystem(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
		init();
		changeState(STATE_SYS);
	}

	private void init()
	{
		m_datas = null;
		m_datas = new byte[3][][];
		m_datas[0] = new byte[STRS_SYSTEM.length][2];
		m_datas[1] = new byte[STRS_CHAT.length][2];
		m_datas[2] = new byte[STRS_DIS.length][2];
		//
     	m_datas[2][0][INDEX_TYPE] = TYPE_STR;
     	m_datas[2][1][INDEX_TYPE] = TYPE_OC;
		//
     	int index = 0;
     	int startIndex = 0;
        while(index < m_datas.length){
        	  int length = m_datas[index].length;
        	  for(int i = 0,j = startIndex;i<length;i++,j++){
        		  m_datas[index][i][1] = GameManager.s_system_infos[j];
        // 		  Tools.debugPrintln("m_datas["+index+"]["+i+"]="+m_datas[index][i][1]);
        	  }
        	  startIndex += length;
        	  index ++;
        }
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)
				||AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
//			clearAllRects();
//			registerNormalSoft();
			
			final UIMessageBox messageBox = (UIMessageBox) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
			final byte[] data = cloneArrary();
			final boolean flag  =  GameManager.getInstance().isChangeSetSystem(data);
			messageBox.setMessage( getCommitStr(false, flag));
			messageBox.addListener(new UIListener() {
				public void actionPerformed()
				{
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						if( flag ){//有改变设置
							GameManager.getInstance().changeSetSystem(data);
						}
						close();
						GameManager.s_isControlledByUi = false;
						UIManager.getInstance().removeWindow(messageBox);
//						GameTouchManager.recoverRect();
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(messageBox);
//						initTouch();
					}
				}
			});
			UIManager.getInstance().addWindow(messageBox);	    
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP) ) {
			if (m_index == 0) {
				return;
			}
			else {
				m_index--;
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN) ) {
			byte[][] datas = getCurrentData();
			if (m_index == datas.length - 1) {
				return;
			}
			else {
				m_index++;
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {
			if (m_state == STATE_SYS) {
				changeState(STATE_DIS);
			}
			else {
				changeState(m_state - 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {
			if (m_state == STATE_DIS) {
				changeState(STATE_SYS);
			}
			else {
				changeState(m_state + 1);
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			clikItem();
		}else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			final UIMessageBox messageBox = (UIMessageBox) UIWindowFactory
					.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
			final boolean flag  =  GameManager.getInstance().isChangeSetSystem(cloneArrary());
			messageBox.setMessage( getCommitStr(true, flag));
			messageBox.addListener(new UIListener() {
				public void actionPerformed()
				{
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						if( flag ){//有改变设置
							GameManager.getInstance().changeSetSystem(cloneArrary());
						}
						close();
						GameManager.s_isControlledByUi = false;
						UIManager.getInstance().removeWindow(messageBox);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(messageBox);
					}
				}
			});
			UIManager.getInstance().addWindow(messageBox);	
		}
	}
    
	private String getCommitStr(boolean isLeft, boolean isChanged)
	{
		if (isLeft) {
			if (isChanged) {
				return "设置发生修改，确定提交?";
			}
			else {
				return "设置未发生修改，确定提交?";
			}
		}
		else {
			if (isChanged) {
				return "设置发生修改，确定退出?";
			}
			else {
				return "设置未发生修改，确定退出?";
			}
		}
	}
	
	private void clikItem()
	{
		byte[][] datas = getCurrentData();
		byte type = datas[m_index][INDEX_TYPE];
		switch (type) {
			case TYPE_OC:
				datas[m_index][INDEX_DATE] = datas[m_index][INDEX_DATE] == (byte) 0 ? (byte) 1 : (byte) 0;
				break;
			case TYPE_HML:
			case TYPE_STR:
				if (datas[m_index][INDEX_DATE] == 2) {
					datas[m_index][INDEX_DATE] = 0;
				}
				else {
					datas[m_index][INDEX_DATE]++;
				}
			case TYPE_NUM:
			default:
				break;
		}
	}
    
	/** 克隆数据到一维数组 */
	private byte[] cloneArrary(){
		int length = m_datas[0].length + m_datas[1].length+m_datas[2].length;
		byte[] temp = new byte[length];
		int startIndex = 0;
	    int index = 0;
	    while( index < m_datas.length ){
	    	for(int i=0,j=startIndex;i<m_datas[index].length;i++,j++){
	    		temp[j] = m_datas[index][i][INDEX_DATE];
	    	}
	    	startIndex += m_datas[index].length;
	    	index++;
	    }		
		return temp;
	}
	
	private byte[][] getCurrentData()
	{
		return m_datas[m_state - STATE_SYS];
	}

	private String[] getCurrentAtts()
	{
		switch (m_state) {
			case STATE_SYS:
				return STRS_SYSTEM;
			case STATE_DIS:
				return STRS_DIS;
			default:
				return STRS_CHAT;
		}
	}

	private void changeState(int state)
	{
		if (m_state == state) {
			return;
		}
		m_state = state;
		m_index = 0;
		
		registerTouch();
	}
	
	///////
	int[][] mlable = null;
	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		mlable = new int[][]{
				{161,38,51,21},
				{216,38,51,21},
				{271,38,51,21},
		};
//		registerLoginBtn(rect);
		initTouchRect(mlable);
		int len = getCurrentAtts().length;
		int[][]content = new int[len][];
		for (int i=0;i<len;i++) {
			content[i] = new int[]{20, 66+i*UITools.H_PRE, 440,UITools.H_PRE};
		}
//		registerContentBtn(content);
		initTouchRect(content);
	}
	protected void onTouchRect(int[][] rect, int touchid) {
		if (rect == mlable) {
			changeState(STATE_SYS + touchid);
		} else {
			if (m_index == touchid) {
				GameTouchManager.keyPressedFire();
			} else {
				m_index = touchid;
			}
		}
	}

	private final String TITLE = "系统设置";
	private final String[] LABELS = { "系统", "聊天", "显示" };//"队伍申请", "切磋开关", "交易开关",
	private final String[] STRS_SYSTEM = { "游戏声音" };
	private final String[] STRS_CHAT = { "世界频道","阵营频道","家族频道","队伍频道","私聊频道","系统频道" };
	private final String[] STRS_DIS = { "显示玩家","天气效果"};

	private final int H_PRE = UITools.H_PRE;
//	private int m_h_content = NUM_MAX_ROWS * H_PRE + (UITools.IH << 1) ;
	
	private final int NUM_LABEL_DIS = 3 ;//显示的标签数目
	
	private final int X_LABEL_FIRST = ((UITools.W_BG - (NUM_LABEL_DIS * UITools.W_LABEL_BIG + (NUM_LABEL_DIS - 1)* UITools.IW_RECT_CONTENT)) >> 1);// 居中显示
	private final int X_ARR_LEFT = X_LABEL_FIRST - (UIManager.IMAGE_LABEL_ARR.getWidth()) - 2;
	private final int X_ARR_RIGHT = UITools.W_BG - X_ARR_LEFT - (UIManager.IMAGE_LABEL_ARR.getWidth());

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE);
		int tempY = m_y + UITools.INITY_CONTENT;
		int tempX = m_x + UITools.INITX_CONTENT;
//		paintLabelArr(g,m_x + X_ARR_LEFT, tempY, true);
		paintLabels(g, m_x + X_LABEL_FIRST, tempY);
//		paintLabelArr(g, m_x + X_ARR_RIGHT, tempY, false);
		tempY += ( UITools.H_LABEL + UITools.IH_RECT_CONTENT) ;
		paintItems(g, tempX, tempY);
		
	}
	
	private void paintLabelArr(Graphics g, int x, int y, boolean isLeft)
	{
		UITools.paintLabelArr(g, x, y, isLeft, true);
	}

	private void paintLabels(Graphics g, int x, int y)
	{
		int tempX = x;
		int w = UITools.W_LABEL_BIG + 4;
		for (int i = 0; i < LABELS.length; i++) {
			boolean isSel= m_state - STATE_SYS == i ;
			UITools.paintLabelStr(g, tempX, y, UITools.W_LABEL_BIG, UITools.H_LABEL, LABELS[i], isSel, isSel);
			tempX += w;
		}
	}

	private String OPEN = "开" ;
	private String CLOSE = "关" ;
	private String LOW = "普通" ;
	private String MID = "高等" ;
	private String HIGH = "优质" ;
	private String STR0 = "最低";
	private String STR1 = "部分";
	private String STR2 = "全部";
	private int W_SEL = UITools.W_CONTENT - (UITools.IW_RECT_CONTENT << 1);
	
	private void paintItems(Graphics g, int x, int y)
	{
		byte[][] datas = getCurrentData();
		String[] atts = getCurrentAtts();
		int tempY = y + 3;
		int tempX = x ;
		int focusY = 0;
		UITools.paintContentRect(g,tempX, tempY, UITools.W_CONTENT, H_RECT) ;
		tempX += UITools.IW_RECT_CONTENT ;
		for (int i = 0; i < datas.length; i++) {
			byte type = datas[i][INDEX_TYPE];
			String value = null;
			switch (type) {
				case TYPE_OC:
					value = datas[i][INDEX_DATE] == 0 ? CLOSE : OPEN;
					break;
				case TYPE_HML:
					switch (datas[i][INDEX_DATE]) {
						case 0:
							value = LOW;
							break;
						case 1:
							value = MID;
							break;
						default:
							value = HIGH;
							break;
					}
					break ;
				case TYPE_STR:
					switch (datas[i][INDEX_DATE]) {
						case 0:
							value = STR0;
							break;
						case 1:
							value = STR1;
							break;
						default:
							value = STR2;
							break;
					}
					break;
				case TYPE_NUM:
				default:
					value = String.valueOf(datas[i][INDEX_DATE]);
					break;
			}
			boolean isSel= m_index == i ;
			paintItem(g,tempX + 5, tempY + 3, atts[i], value,isSel);
			if (isSel) {
				focusY =tempY ;
			}
			tempY += H_PRE;
		}
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
		UITools.drawSelectBox(g, tempX, focusY+1, W_SEL+1, H_PRE);
	}

	private void paintItem(Graphics g, int x, int y, String att, String value, boolean isSel)
	{
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString(att, x, y, 0);
		g.drawString(value, x + 160, y, Graphics.RIGHT | Graphics.TOP);
	}
}
